/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jbr.simplefoodplan.gwt.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.jbr.simplefoodplan.gwt.client.Dish;
import com.jbr.simplefoodplan.gwt.client.MainEntryPoint;
import com.jbr.simplefoodplan.gwt.client.ServerCom;
import com.jbr.simplefoodplan.gwt.client.ServerCom.ElementCallback;


/**
 *
 * @author jbr
 */
public final class DishUi extends Composite {
    private static DishUiUiBinder uiBinder = GWT.create(DishUiUiBinder.class);
    private final MainEntryPoint main;

    interface DishUiUiBinder extends UiBinder<Widget, DishUi> {
    }

    @UiField Button close;
    @UiField DialogBox dialogBox;
    @UiField ListBox dishes;
    @UiField TextBox dishname;

    public void updateDishes(MainEntryPoint main) {
        int selected = dishes.getSelectedIndex();
        dishes.clear();
        dishes.addItem("Ny..", "new");
        for (Dish dish : main.dishList) {
            String lastUsageInDays = dish.getLastUsageInDays();
            if (lastUsageInDays == null) {
                lastUsageInDays = "ikke brugt";
            }
            dishes.addItem(dish.getName(), dish.getId());
        }
        dishes.setSelectedIndex(selected==-1 ? 0 : selected);
        handleDishChange(null);
    }

    public DishUi(MainEntryPoint main) {
        this.main = main;
        initWidget(uiBinder.createAndBindUi(this));
        System.out.println("DishUi initiated");
        updateDishes(main);
    }

    @UiHandler("dishes")
    void handleDishChange(ChangeEvent e) {
        if (dishes.getSelectedIndex()>-1) {
            System.out.println("Der blev valgt: " +
                dishes.getItemText(dishes.getSelectedIndex()));
            dishname.setText(dishes.getItemText(dishes.getSelectedIndex()));
        }
        else
            System.out.println("Der blev klikket, men ikke valgt noget");
    }

    @UiHandler("close")
    void handleClose(ClickEvent e) {
        dialogBox.hide();
    }

    @UiHandler("dishname")
    void handleDishnameChange(ChangeEvent e) {
        System.out.println("Changed dishname to " + dishname.getText());
        Dish dishToEdit;
        if (dishes.getValue(dishes.getSelectedIndex()).equalsIgnoreCase("new"))
            dishToEdit = (Dish) JavaScriptObject.createObject();
        else
            dishToEdit = main.dishList.get(dishes.getSelectedIndex()-1);
        
        dishToEdit.setName(dishname.getText());
        ServerCom.put(dishes.getSelectedIndex(),
                dishToEdit,
                new ElementCallback<Dish, Integer>() {

            @Override
            public void onResponseOk(Dish element, Integer backref) {
                System.out.println("Response ok. Dish added");
                main.updateDishList(new ChangeHandler() {

                    @Override
                    public void onChange(ChangeEvent event) {
                        updateDishes(main);
                    }
                });
            }
        });
    }

    public void show() {
        dialogBox.center();
    }

    public void hide() {
        dialogBox.hide();
    }



    

}
